package com.wish.g04.base.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.dto.JrFactoringDTO;
import com.wish.g04.base.entity.CfContract;
import com.wish.g04.base.mapper.CfContractMapper;
import com.wish.g04.base.service.ICfContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.g04.base.vo.FactoringDetailVO;
import com.wish.g04.common.core.utils.DateUtils;
import com.wish.g04.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 薛凯
 * @since 2022-05-23
 */
@Service
public class CfContractServiceImpl extends ServiceImpl<CfContractMapper, CfContract> implements ICfContractService {

    /**
     * 申请保理
     * 合同查询
     * @param jrFactoringDTO
     * @return
     */
    @Override
    public List<CfContract> getList(JrFactoringDTO jrFactoringDTO) {
        QueryWrapper<CfContract> wrapper = new QueryWrapper<>();
                // 未申请过租房分期
        wrapper.eq("rent_installment_flg",0)
                // 未申请过租约保理
                .eq("zybl_flg",0)
                // 合同为正常执行
                .eq("contract_state",2)
                // 未续约
                .eq("xuyue_state",0)
                // 未冻结
                .eq("freeze_status",0)
                //合同开始日小于等于当前日期
                .le("start_date", LocalDateTime.now())
                // 剩余租期不少于3个月 按90天筛选
                .gt("end_date", DateUtil.offsetDay(DateUtil.date(),60));
        if (StringUtils.isNotNull(jrFactoringDTO.getPremiseName())) {
            wrapper.like("premise_name",jrFactoringDTO.getPremiseName());
        }
        if (StringUtils.isNotNull(jrFactoringDTO.getTenantName())) {
            wrapper.like("tenant_name",jrFactoringDTO.getTenantName());
        }
        wrapper.orderByDesc("id");
        return list(wrapper);
    }

    /**
     * 设置信息
     *
     * @param factoringDetailVO
     * @return
     */
    @Override
    public FactoringDetailVO setMessage(FactoringDetailVO factoringDetailVO) {
        CfContract contract = getById(factoringDetailVO.getContractId());
        factoringDetailVO.setTenantPhone(contract.getTenantPhone())
                .setHouseId(contract.getHouseId())
                .setHouseName(contract.getHouseName());
        return factoringDetailVO;
    }
}
